-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pthreads support for Node.js #9745
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
var nodeFS = require('fs'); | ||
|
||
var nodeRead = function(filename) { | ||
return nodeFS.readFileSync(filename).toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nodeFS.readFileSync(filename).toString(); | |
return nodeFS.readFileSync(filename, 'utf8'); |
(Just a minor thing, it avoids creating an intermediate Buffer
/copying it into a string)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks! I think it's best to open a separate PR for that later as I see we have more than one existing call like that that could be improved that way.
@@ -0,0 +1,62 @@ | |||
// Copyright 2019 The Emscripten Authors. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have tests/pthread/test_pthread_create.cpp already. How does tests/core/pthread relate to the tests in tests/pthread?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests/pthread/
are existing tests, that only work on the web. The new core/pthreads
are for the core test suite and run on node.
Sadly the web tests can't just be reused as they make some web assumptions (like reporting the result using an xhr, when it's ok to block and when not, etc., which it turns out node differs on).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. We should probably document somewhere (even if it's just for our own testing purposes) exactly the differences between web-pthreads and node-pthreads environments, and it would be nice to eventually unify the tests. But that doesn't need to be in this PR.
tests/core/pthread/create.cpp
Outdated
CreateThread(i); | ||
} | ||
|
||
// Node.js requires the event loop to be reached for the worker to start up. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that PTHREAD_POOL doesn't work for node?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, it should. It didn't in earlier work here, but looks like the current PR got it to work! I'm adding a test for the pool allowing works to be synchronously available.
Hi @kripken, thanks for your work on this! I'm running into an issue and I wanted to check in with you before working on a fix. I'm currently deploying a multithreaded application using Emscripten inside Electron, and this change appears to have broken things for me - I get errors like The simplest solution I can think of is to switch the priorities, so web workers are preferred over node workers. Or additional checks could be added to confirm that the Node worker_threads API is available before choosing to use it. What do you think? If it would be helpful I could put together a minimal test app demonstrating the problem. |
@abram Interesting, I didn't realize how complex this could be I guess 😄 Are you saying we might check, in |
@kripken Yes, that sounds right. I'll work on a PR. Thanks! |
This ended up tricky because of various node/web differences, like the global scope, the event loop, APIs, etc. With help on emscripten-core#6567 I think this PR finally manages to make it work, at least for an initial "hello world" type test. Thanks to everyone on that issue and in particular @addaleax! This moves some code from shell.js into side .js files, so that we can use it in multiple places. Fixes emscripten-core#6567 but as we add more tests I'm sure we'll find more issues, this just shows basic functionality so far. We may not be able to easily reuse the existing browser tests as they have browser-specific things in them, but we can try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node -v
npm -v
@@ -432,6 +432,18 @@ var LibraryPThread = { | |||
worker.onerror = function(e) { | |||
err('pthread sent an error! ' + e.filename + ':' + e.lineno + ': ' + e.message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Storage Allocation Method. Hardware is needed.. static Ram. And bios need to be on an external harddrive. .3 disk and 5 readers . With the modifications of each disc except for the center disc need to rotate at a syncrinive rpm in opposite directions this the nodes and threads will be will be twin cousins to the interface OS.. thus the external hardware will see them as one until data is deleted... The electron nodes only take in and recognize hardware attributed firmware ,threads don't recognize any thing except nodes. Environment is semi magnetic before and virtual after... Thus nodes and threads need to be external of an O.S,. [∆π¥] ..Z +Z²=©°![Uploading b5{3c0afaa} (ae8ba507-5d6d-488c-88eb-8b4ffa783c47 id=0x7f0a0v.www.darpa.mil
ret = tryParseAsDataURI(filename); | ||
if (!ret) { | ||
#endif | ||
if (!nodeFS) nodeFS = require('fs'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/node_shell_read.js
This ended up tricky (see the dates on the commits...) because of various node/web differences, like the global scope, the event loop, APIs, etc. With help on #6567 I think this PR finally manages to make it work, at least for an initial "hello world" type test. Thanks to everyone on that issue and in particular @addaleax!
Followups should add a lot more tests. Sadly I don't think we can just reuse the existing browser tests, as they make some web assumptions (like reporting the result using an xhr, when it's ok to block and when not, etc.).
This moves some code from shell.js into side .js files, so that we can use it in multiple places.
Fixes #6567